Hitachi Microcomputer Support Software SH Series C Compiler User's Manual HS0700CLCU4SE Rev. 4.0 4/9/97 Hitachi, Ltd. Notice When using this document, keep the following in mind: 1. This document may, wholly or partially, be subject to change without notice. 2. All rights are reserved: No one is permitted to reproduce or duplicate, in any form, the whole or part of this document without Hitachi's permission. 3. Hitachi will not be held responsible for any damage to the user that may result from accidents or any other reasons during operation of the user's unit according to this document. 4. Circuitry and other examples described herein are meant merely to indicate the characteristics and performance of Hitachi's semiconductor products. Hitachi assumes no responsibility for any intellectual property claims or other problems that may result from applications based on the examples described herein. 5. No license is granted by implication or otherwise under any patents or other rights of any third party or Hitachi, Ltd. 6. MEDICAL APPLICATIONS: Hitachi's products are not authorized for use in MEDICAL APPLICATIONS without the written consent of the appropriate officer of Hitachi's sales company. Such use includes, but is not limited to, use in life support systems. Buyers of Hitachi's products are requested to notify the relevant Hitachi sales offices when planning to use the products in MEDICAL APPLICATIONS. Preface This manual explains the facilities and operating procedures for the SH series C compiler. Please read this manual and the related manuals listed below before using the C compiler to fully understand the system. The C compiler translates source programs written in C into relocatable object programs or assembly source programs for Hitachi superH RISC engine family microcomputers (SH1, SH2, SH3, and SH3E). Features of this compiler system are as follows: 1. generates an object program that can be written to ROM to be installed in a user system. 2. supports an optimization option that increases execution speed of object programs or minimizes program size. 3. supports a debugging-information output function for a C source level debugging or C source analysis using a debugger . 4. selects an assembly source program or relocatable object program and outputs it. This manual consists of four parts and appendixes. The information contained in each part is summarized below. 1. PART I OVERVIEW AND OPERATIONS The overview sections cover C compiler functions and developing procedures. The operation sections cover how to invoke the compiler, how to specify optional functions, and how to interprete listings created by the C compiler. 2. PART II C PROGRAMMING This part explains the limitations of the C compiler and the special factors in object program execution which should be considered when creating a program. 3. PART III SYSTEM INSTALLATION This part explains the object program being written in ROM and memory allocation when installing an object program generated by the C compiler on a system. In addition, specifications of the low-level interface routine must be made by the user when using C language standard I/O library and memory management library. 4. PART IV ERROR MESSAGES This part explains the error messages corresponding to compilation errors and the standard library error messages corresponding to run time errors. This manual describes the SH C compiler that operates on UNIX*1, or MS-DOS* 2 that runs (operates) on the IBM-PC*3 and PC compatibles. In this manual, compilers functioning on a UNIX system are referred to as UNIX version and compilers functioning on an MS-DOS system are referred to as PC systems. Notes on Symbols: The following symbols are used in this manual. Symbols Used in This Manual Symbol Explanation <> Indicates an item to be specified. [] Indicates an item that can be omitted. ... Indicates that the preceding item can be repeated. Indicates one or more blanks. (RET) Indicates the carriage return key (return key). | Indicates that one of the items must be selected. (CNTL) Indicates that the control key should be held down while pressing the key that follows. Notes: 1. UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Limited. 2. MS-DOS is an operating system administrated by Microsoft Corporation. 3. IBM PC is a registered trademark of International Business Machines Corporation. Related Manuals: Refer to the following manuals together with the SH Series C Compiler when creating a program using the C compiler. SH Series Cross Assembler User's Manual SH Series Simulator/Debugger User's Manual Integrated Manager User's Manual H Series Linkage Editor User's Manual H Series Librarian User's Manual E7000 SH7032, SH7034 Emulator User's Manual E7000 SH7604 Emulator User's Manual E7000 SH7708 Emulator User's Manual Refer to the following manuals for details on the SH instruction execution: SH7000 Series Programming Manual SH7000/SH7600 Series Programming Manual SH7700 Series Programming Manual Contents Part I OVERVIEW AND OPERATIONS.................................................................... 1 Section 1 Overview.............................................................................................................. 3 Section 2 Developing Procedures.................................................................................... 5 Section 3 C Compiler Execution ..................................................................................... 7 7 7 7 8 8 9 10 20 21 23 23 24 26 28 29 30 31 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 How to Invoke the C Compiler.......................................................................................... 3.1.1 Compiling Programs ............................................................................................ 3.1.2 Displaying Command Line Format and Compiler Options ................................. 3.1.3 C Compiler Options.............................................................................................. 3.1.4 Compiling Multiple C Programs .......................................................................... Naming Files...................................................................................................................... Compiler Options .............................................................................................................. Option Combinations......................................................................................................... Correspondence to Standard Libraries .............................................................................. C Compiler Listings .......................................................................................................... 3.6.1 Structure of C Compiler Listings ......................................................................... 3.6.2 Source Listing ...................................................................................................... 3.6.3 Object Listing ....................................................................................................... 3.6.4 Statistics Information............................................................................................ 3.6.5 Command Line Specification ............................................................................... C Compiler Environment Variables .................................................................................. Implicit Declaration by Option.......................................................................................... Part II C PROGRAMMING ............................................................................................. 33 Section 1 Limits of the C Compiler ................................................................................ 35 Section 2 Executing a C Program ................................................................................... 37 2.1 2.2 2.3 Structure of Object Programs ............................................................................................ 38 Internal Data Representation ............................................................................................. 41 2.2.1 Scalar-Type Data.................................................................................................. 42 2.2.2 Combined-Type Data ........................................................................................... 43 2.2.3 Bit Fields .............................................................................................................. 45 2.2.4 Memory Allocation of Little Endian .................................................................... 48 Linkage with Assembly Programs..................................................................................... 50 2.3.1 External Identifier Reference ............................................................................... 50 2.3.2 Function Call Interface ......................................................................................... 52 Section 3 Extended Specifications .................................................................................. 61 3.1 Interrupt Functions ............................................................................................................ 3.1.1 Description ........................................................................................................... 3.1.2 Explanation........................................................................................................... 3.1.3 Notes..................................................................................................................... 3.2 Intrinsic Functions ............................................................................................................. 3.2.1 Intrinsic Functions................................................................................................ 3.2.2 Description ........................................................................................................... 3.2.3 Intrinsic Function Specifications.......................................................................... 3.2.4 Notes..................................................................................................................... 3.2.5 Example................................................................................................................ 3.2.6 Dividing .......................................................................................... 3.3 Section Change Function................................................................................................... 3.3.1 Description ........................................................................................................... 3.3.2 Explanation........................................................................................................... 3.3.3 Notes..................................................................................................................... 3.3.4 Example................................................................................................................ 3.4 Single-Precision Floating-Point Library............................................................................ 3.4.1 Description ........................................................................................................... 3.4.2 Notes..................................................................................................................... 3.5 Japanese Description in String Literals ............................................................................. 3.6 Inline Function................................................................................................................... 3.6.1 Description ........................................................................................................... 3.6.2 Explanation........................................................................................................... 3.6.3 Notes..................................................................................................................... 3.6.4 Example................................................................................................................ 3.7 Inline Expansion in Assembly Language .......................................................................... 3.7.1 Description ........................................................................................................... 3.7.2 Explanation........................................................................................................... 3.7.3 Notes..................................................................................................................... 3.7.4 Example................................................................................................................ 3.8 Specifying Two-byte Address Variables ........................................................................... 3.8.1 Description ........................................................................................................... 3.8.2 Explanation........................................................................................................... 3.8.3 Notes..................................................................................................................... 3.9 Specifying GBR Base Variables........................................................................................ 3.9.1 Description ........................................................................................................... 3.9.2 Explanation........................................................................................................... 3.9.3 Notes..................................................................................................................... 3.10 Register Save and Recovery Control................................................................................. 3.10.1 Description............................................................................................................ 3.10.2 Explanation........................................................................................................... 3.10.3 Notes..................................................................................................................... 3.10.4 Example................................................................................................................ 61 61 63 65 66 66 66 66 71 72 73 74 74 74 74 74 75 75 75 77 78 78 78 78 78 79 79 79 79 80 81 81 81 81 82 82 82 82 83 83 83 83 84 3.11 Global Variable Register Allocation ................................................................................. 3.11.1 Description ........................................................................................................... 3.11.2 Explanation........................................................................................................... 3.11.3 Notes..................................................................................................................... 3.11.4 Example................................................................................................................ 84 84 84 85 85 Section 4 Notes on Programming.................................................................................... 87 4.1 4.2 Coding Notes ..................................................................................................................... 87 4.1.1 float Type Parameter Function ............................................................................. 87 4.1.2 Program Whose Evaluation Order is Not Regulated............................................ 87 4.1.3 Overflow Operation and Zero Division................................................................ 88 4.1.4 Assignment to const Variables ............................................................................. 89 4.1.5 Precision of Mathematical Function Libraries ..................................................... 89 Notes on Programming Development................................................................................ 90 Part III SYSTEM INSTALLATION.............................................................................. 93 Section 1 Overview of System Installation .................................................................. 95 Section 2 Allocating Memory Areas ............................................................................. 97 2.1 2.2 Static Area Allocation........................................................................................................ 2.1.1 Data to be Allocated in Static Area ...................................................................... 2.1.2 Static Area Size Calculation................................................................................. 2.1.3 ROM and RAM Allocation .................................................................................. 2.1.4 Initialized Data Area Allocation .......................................................................... 2.1.5 Memory Area Allocation Example and Address Specification at Program Linkage ................................................................................................................. Dynamic Area Allocation.................................................................................................. 2.2.1 Dynamic Areas ..................................................................................................... 2.2.2 Dynamic Area Size Calculation ........................................................................... 2.2.3 Rules for Allocating Dynamic Area ..................................................................... 97 97 97 100 100 100 102 102 102 105 Section 3 Setting the Execution Environment ............................................................. 107 3.1 3.2 3.3 Vector Table Setting (VEC_TBL)..................................................................................... 108 Initialization (_ _INIT) ...................................................................................................... 109 Section Initialization (_ _INITSCT).................................................................................. 110 Section 4 Setting the C Library Function Execution Environment ...................... 113 4.1 4.2 4.3 4.4 Vector Table Setting (VEC_TBL)..................................................................................... Initializing Registers (_ _INIT) ......................................................................................... Initializing Sections (_ _INITSCT) ................................................................................... Initializing C Library Functions (_ _INITLIB) ................................................................. 4.4.1 Creating Initialization Routine (_INIT_IOLIB) for Standard I/O Library Function................................................................................................... 115 115 115 116 117 4.4.2 4.5 4.6 Creating Initialization Routine (_INIT_OTHERLIB) for Other Library Function................................................................................................... Closing Files (_ _CLOSEALL) ......................................................................................... Creating Low-Level Interface Routines ............................................................................ 4.6.1 Concept of I/O Operations.................................................................................... 4.6.2 Low-Level Interface Routine Specifications........................................................ 119 120 121 122 123 Part IV ERROR MESSAGES .......................................................................................... 131 Section 1 Error Messages .................................................................................................. 133 Section 2 C Standard Library Error Messages ............................................................ 153 APPENDIX .............................................................................................................................. 157 Appendix A Language and Standard Library Function Specifications of the C Compiler ........................................................................................ 159 A.1 A.2 A.3 Language Specifications of the C Compiler...................................................................... A.1.1 Compilation Specifications ................................................................................. A.1.2 Environmental Specifications .............................................................................. A.1.3 Identifiers ............................................................................................................ A.1.4 Characters ............................................................................................................. A.1.5 Integer................................................................................................................... A.1.6 Floating-Point Numbers ....................................................................................... A.1.7 Arrays and Pointers .............................................................................................. A.1.8 Register................................................................................................................. A.1.9 Structure, Union, Enumeration, and Bit Field Types ........................................... A.1.10 Qualifier................................................................................................................ A.1.11 Declarations.......................................................................................................... A.1.12 Statement.............................................................................................................. A.1.13 Preprocessor ......................................................................................................... C Library Function Specifications..................................................................................... A.2.1 stddef.h ................................................................................................................ A.2.2 assert.h ................................................................................................................. A.2.3 ctype.h .................................................................................................................. A.2.4 math.h ................................................................................................................... A.2.5 setjmp.h ................................................................................................................ A.2.6 stdio.h ................................................................................................................... A.2.7 string.h.................................................................................................................. A.2.8 errno.h .................................................................................................................. A.2.9 Libraries that are Not Supported by the SH C Compiler...................................... Floating-Point Number Specifications .............................................................................. A.3.1 Internal Representation of Floating-Point Numbers ............................................ A.3.2 float....................................................................................................................... 159 159 159 160 161 162 163 164 164 165 165 166 166 167 168 168 168 168 169 169 170 171 172 173 174 174 176 A.3.3 A.3.4 double and long double ........................................................................................ 177 Floating-point Operation Specifications .............................................................. 179 Appendix B Parameter Allocation Example ................................................................ 183 Appendix C Usage of Registers and Stack Area ........................................................ 187 Appendix D Creating Termination Functions ............................................................. 189 D.1 D.2 D.3 Creating Library onexit Function ...................................................................................... 189 Creating exit Function ....................................................................................................... 190 Creating Abort Routine...................................................................................................... 192 Appendix E Examples of Low-Level Interface Routine .......................................... 193 Appendix F ASCII Codes .................................................................................................. 199 Index ........................................................................................................................................... 201 Figures Part I Figure 1.1 Figure 1.2 Figure 1.3 Figure 1.4 Figure 1.5 Figure 1.6 Figure 1.7 Figure 1.8 C Compiler Functions ............................................................................................. Relationship between the C Compiler and Other Software.................................... Source Listing Output for show = noinclude, noexpansion.................................... Source Listing Output for show = include, expansion............................................ Object Listing Output for show = source, object.................................................... Object Listing Output for show = nosource, object................................................ Statistics Information.............................................................................................. Command Line Specification ................................................................................. 3 5 24 25 26 27 28 29 Allocation and Deallocation of a Stack Frame ....................................................... Parameter Area Allocation...................................................................................... Example of Allocation to Parameter Registers....................................................... Return Value Setting Area Used When Return Value Is Written to Memory........ Stack Processing by an Interrupt Function ............................................................. 52 57 59 60 64 Section Size Information ........................................................................................ Static Area Allocation............................................................................................. Nested Function Calls and Stack Size .................................................................... Program Configuration (No C Library Function is Used)...................................... Program Configuration When C Library Functions are Used ................................ FILE-Type Data...................................................................................................... 97 101 104 107 113 119 Part II Figure 2.1 Figure 2.2 Figure 2.3 Figure 2.4 Figure 2.5 Part III Figure 3.1 Figure 3.2 Figure 3.3 Figure 3.4 Figure 3.5 Figure 3.6 Appendix Figure A.1 Figure C.1 Structure for the Internal Representation of Floating-Point Numbers.................... 174 Usage of Registers and Stack Area......................................................................... 187 Tables Part I Table 1.1 Table 1.2 Table 1.3 Table 1.4 Table 1.5 Table 1.6 Table 1.7 Table 1.8 Standard File Extensions Used by the C Compiler ................................................ 9 C Compiler Options................................................................................................ 10 Macro Names, Names, and Constants Specified by the Define Option ................. 15 Option Combinations.............................................................................................. 20 Correspondence between Standard Libraries and Compile Options ...................... 22 Structure and Contents of C Compiler Listings...................................................... 23 Environment Variables ........................................................................................... 30 Implicit Declaration ................................................................................................ 31 Part II Table 2.1 Table 2.2 Table 2.3 Table 2.4 Table 2.5 Table 2.6 Table 2.7 Table 2.8 Table 2.9 Table 2.10 Table 2.11 Table 2.12 Table 2.13 Limits of the C Compiler........................................................................................ 35 Memory Area Types and Characteristics................................................................ 39 Internal Representation of Scalar-Type Data ......................................................... 42 Internal Representation of Combined-Type Data ................................................... 43 Bit Field Member Specifications ............................................................................ 45 Rules on Changes in Registers After a Function Call ............................................ 53 General Rules on Parameter Area Allocation......................................................... 58 Return Value Type and Setting Area...................................................................... 60 Interrupt Specifications........................................................................................... 62 Intrinsic Functions .................................................................................................. 67 Function List of Single-Precision Floating-Point Library ...................................... 76 Default Settings of Japanese Code.......................................................................... 77 Troubleshooting...................................................................................................... 90 Part III Table 3.1 Table 3.2 Stack Size Calculation Example ............................................................................. 104 Low-Level Interface Routines ................................................................................ 121 Appendix Table A.1 Table A.2 Table A.3 Table A.4 Table A.5 Table A.6 Table A.7 Table A.8 Table A.9 Table A.10 Table A.11 Table A.12 Compilation Specifications..................................................................................... Environmental Specifications ................................................................................. Identifier Specifications ......................................................................................... Character Specifications ......................................................................................... Integer Specifications.............................................................................................. Integer Types and Their Corresponding Data Range ............................................. Floating-Point Number Specifications.................................................................... Limits on Floating-Point Numbers ......................................................................... Array and Pointer Specifications ............................................................................ Register Specifications............................................................................................ Specifications for Structure, Union, Enumeration, and Bit Field Types ................ Qualifier Specifications .......................................................................................... 159 159 160 161 162 162 163 163 164 164 165 165 Table A.13 Table A.14 Table A.15 Table A.16 Table A.17 Table A.18 Table A.19 Table A.20 Table A.21 Table A.22 Table A.23 Table A.24 Table A.25 Table A.26 Table A.27 Declaration Specifications ...................................................................................... Statement Specifications......................................................................................... Preprocessor Specifications .................................................................................... stddef.h Specifications............................................................................................ assert.h Specifications............................................................................................. ctype.h Specifications ............................................................................................. Set of Characters that Returns True........................................................................ math.h Specifications.............................................................................................. setjmp.h Specifications ........................................................................................... stdio.h Specifications.............................................................................................. Infinity and Not a Number...................................................................................... string.h Specifications............................................................................................. errno.h Specifications ............................................................................................. Libraries that are Not Supported by the SH C Compiler........................................ Types of Values Represented by Floating-Point Numbers..................................... 166 166 167 168 168 168 169 169 169 170 171 171 172 173 175 PART I OVERVIEW AND OPERATIONS Section 1 Overview The SH series C compiler converts source programs written in C to SH series relocatable object programs or assembly source programs. The C compiler supports the SH1, SH2, SH3, and SH3E microcomputers (collectively referred to as SH). Figure 1.1 shows C compiler functions. SH series C compiler SH relocatable object program C source program SH assembly source program Figure 1.1 C Compiler Functions A standard library file (a group of C language level functions that is used in C language program as standard) is also provided in addition to the C compiler. 3 Section 2 Developing Procedures Figure 1.2 shows the relationship between the C compiler package and other software for program development. The C compiler package includes the software enclosed by the dotted line. C source file creation *2 User include file User assembly source program Assembly source program SH series cross assembler Routine created by user H series object converter S-type load module SH series C compiler *1 Software included in the package Standard include file *3 Relocatable object program H series linkage editor *4 Standard library file Load module SH series simulator/debugger Target system Notes: 1. Assembly source programs are output depending on option specification. 2. The standard include file defines C library functions and their macro names in order to use C library functions. 3. Debug information can also be added depending on option specification. 4. A function group, consisting of C library functions and run time routines, is used as standard in the C program. (Refer to section 2.1, Static Area Allocation, in part III, SYSTEM INSTALLATION.) Figure 1.2 Relationship between the C Compiler and Other Software 5 Section 3 C Compiler Execution This section explains how to invoke the C compiler, specify C compiler options, and interpret C compiler listings. 3.1 How to Invoke the C Compiler The format for the command line used to invoke the C compiler is as follows. shc[